const express = require('express')
const config = require(process.cwd() + '/src/config/config')
const fileUtils = require(process.cwd() + '/src/utils/file')
const Game = require(process.cwd() + '/src/class/index')

let router = express.Router()

/* GET users listing. */
router.post('/', function(req, res) {
  let env = new Game.Env()
  // 先创建分类资源文件夹
  env.init(req.body.oldName).then(() => {
    env.name = req.body.newName
    return env.save()
  })
  .then(() => {
    // 删除旧文件
    return fileUtils.deleteDir(config.envPath + req.body.oldName + '.json')
  })
  .then(() => {
    res.sendStatus(200)
  })
  .catch((err) => {
    console.log(err)
    res.sendStatus(500)
  })
})

module.exports = router
